/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package localAI.agents;

import localAI.env.def.Direction;
import localAI.env.def.Orientation;
import localAI.env.Enviroment;

/**
 *
 * @author orcas
 */
public class AutomatonAgent extends Agent {

    private enum STATE {
        GOING_LEFT, GOING_RIGHT, TURNING, ERROR;  //; is optional
    }
    private STATE state;

    public AutomatonAgent(String name, int row, int col, Orientation orientation) {
        super(name, row, col, orientation);
        state = STATE.GOING_LEFT;
    }

    public Direction getMove(Enviroment env) {
        Direction move = null;
        System.out.print("STATE = "+state+", ");
        switch (state) {
            case GOING_LEFT:
                if (env.isCellFree(getRow(), getCol() - 1)) {
                    move = Direction.fwd;
                } else {
                    state = STATE.TURNING;
                    move = Direction.right;
                }
                break;
            case GOING_RIGHT:
                if (env.isCellFree(getRow(), getCol() + 1)) {
                    move = Direction.fwd;
                } else {
                    state = STATE.TURNING;
                    move = Direction.right;
                }
                break;
            case TURNING: {
                Orientation orientation = getOrientation();
                if (orientation.equals(Orientation.North) || orientation.equals(Orientation.South)) {
                    move = Direction.right; //keep turning!
                } else { //start going
                    if (orientation.equals(Orientation.West)) {
                        state = STATE.GOING_LEFT;
                    } else if (getOrientation().equals(Orientation.East)) {
                        state = STATE.GOING_RIGHT;
                    }
                    else state=STATE.ERROR;
                    move = Direction.fwd;
                }
                break;
            }
        }
        System.out.println("move = "+move.toString());
        return move;
    }
}
